<?php
global $_W, $_GPC;
$config = $this->getConfig($_W['uniacid']);
//手机号，短信验证码
$uniacid = $_GPC['i'];
$mobile = $_GPC['phone'];
$smsCode = $_GPC['code'];
if (!$mobile || !$smsCode) {
    return $this->getresult(0, '手机号或验证码为空', ['code' => 1003, 'msg' => '手机号或验证码为空']);
}
//验证手机号
if ((strlen($mobile) != 11) || !$this->is_Mobile($mobile)) {
    return $this->getresult(0, '手机号格式错误', ['code' => 1003, 'msg' => '手机号格式错误']);
}
//验证验证码
$checkCode = pdo_fetch('select * from ' . tablename('goouc_class_phonecode') . ' where weid = :weid AND phone = :phone order by id desc', array(':weid' => $uniacid, ':phone' => $mobile));

if (($checkCode['createtime'] + 300) < time()) {
    return $this->getresult(0, '验证码已过期', ['code' => 1003, 'msg' => '验证码已过期']);
}
if ($checkCode['code'] != $smsCode) {
    return $this->getresult(0, '验证码错误', ['code' => 1003, 'msg' => '验证码错误']);
}
//验证成功
$checkM = pdo_get('goouc_class_user', array('phone' => $mobile, 'weid' => $uniacid), array('id','status'));
if (!$checkM) {
    $data = [
        'weid' => $_W['uniacid'],
        'phone' => $mobile,
        'createtime' => time(),
        'loginstatus' => 1
    ];
    $res = pdo_insert($this->t_user, $data);
    if ($res) {
        $uid = pdo_insertid();
        //注册成功，返兑换币
        if ($config['nstatus'] == 1) {
            pdo_update($this->t_user, array('card' => $config['nnum']), array('id' => $uid));
        }
        //给用户生成token验证值
        $token = $this->createtoken();
        $dat = [
            'weid' => $_W['uniacid'],
            'uid' => $uid,
            'token' => $token,
            'createtime' => time()
        ];
        //插入user_token数据表
        $ress = pdo_insert($this->t_user_token, $dat);
        if (!$ress) {
            return $this->getresult(0, '生成token失败了,请重新登录', ['code' => 4001, 'uid' => $uid]);
        }
        return $this->getresult(0, '返回用户id以及token', ['uid' => $uid, 'code' => 200, 'token' => $token]);
    } else {
        return $this->getresult(0, '看数据', ['msg' => '新用户注册失败', 'code' => 1003]);
    }
} else {
    if ($checkM['status'] == 0) {
        return $this->getresult(0, '账户被禁用，暂时不可登录小程序',['msg' => '账户被禁用，暂时不可登录小程序', 'code' => 1003]);
    }else{
        //给用户生成token验证值
        $token = $this->createtoken();
        //插入user_token数据表
        $has_token = pdo_get($this->t_user_token, ['weid' => $_W['uniacid'], 'uid' => $checkM['id']],array('id'));
        if ($has_token) {
            $ress = pdo_update($this->t_user_token, array('token'=>$token,'createtime'=>time()), array('id' => $has_token['id']));
        } else {
            //插入user_token数据表
            $dat = [
                'weid' => $_W['uniacid'],
                'uid' => $checkM['id'],
                'token' => $token,
                'createtime' => time()
            ];
            $ress = pdo_insert($this->t_user_token, $dat);
        }
        if (!$ress) {
            return $this->getresult(0, '生成token失败了,请重新登录', ['code' => 4001, 'uid' => $checkM['id']]);
        }
        return $this->getresult(0, '登陆成功', ['code' => 200,'uid' => $checkM['id'], 'token' => $token]);
    }
}